﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class SceneMonoManager : LogicModuleBase, ISceneMonoManager
{
    public AllSceneMono allSceneMono;
    public AllSceneMono GetAllSceneMono()
    {
        return allSceneMono;
    }
    public override void Init()
    {
        base.Init();
        if (!RegisterData(out allSceneMono))
        {
            InitAllData();
        }
    }
    private void InitAllData()
    {
        allSceneMono = new AllSceneMono();
    }
    public override void FixedUpdate()
    {
        base.FixedUpdate();
        UpdateAllSceneMono();
    }
    
    private void UpdateAllSceneMono()
    {
        //每帧最大实例化个数
        int maxCount = 35;
        List<string> Keys = new List<string>();
        foreach (string key in allSceneMono._ObjList.Keys)
        {
            Keys.Add(key);
        }
        for(int i=0;i< Keys.Count; i++)
        {
            if (!allSceneMono._sceneMonoDatas.ContainsKey(Keys[i]))
            {
                RemoveSceneObj(Keys[i]);
            }
        }
        foreach (string key in allSceneMono._sceneMonoDatas.Keys)
        {
            if (maxCount < 0)
                break;
            if (allSceneMono._ObjList.ContainsKey(key))
            {
                if (allSceneMono._ObjList[key] == null)
                {
                    allSceneMono._ObjList.Remove(key);
                    continue;
                }
            }
            else
            {
                maxCount--;
                AddSceneObj(key,out GameObject Clone);
            }
            UpdateTheObj(key);
        }
    }
    private void UpdateTheObj(string key)
    {
        allSceneMono._ObjList[key].transform.position = allSceneMono._sceneMonoDatas[key].position;
//         if (allSceneMono._sceneMonoDatas[key].shape == Shape.Circle)
//         {
//             allSceneMono._ObjList[key].GetComponent<CircleCollider2D>().radius = allSceneMono._sceneMonoDatas[key].size.x;
//         }
//         if (allSceneMono._sceneMonoDatas[key].shape == Shape.Square)
//         {
//             allSceneMono._ObjList[key].GetComponent<BoxCollider2D>().size = allSceneMono._sceneMonoDatas[key].size;
//         }
    }

    /// <summary>
    /// 添加一个对象
    /// </summary>
    /// <param name="mono"></param>
    public void AddSceneMono(string Name, SceneMonoData SM)
    {
        if (!Name.Contains("("))
        {
            Name = Name + " (0)";
        }
        int i = 0;
        string name = Name;
        while (allSceneMono._sceneMonoDatas.ContainsKey(name))
        {
            i++;
            name = Name.Split('(')[0] + "(" + i + ")";
        }
        Name = name;
        allSceneMono._sceneMonoDatas.Add(Name, SM);
    }
    /// <summary>
    /// 添加一个对象并获得该对象名字
    /// </summary>
    /// <param name="Name"></param>
    /// <param name="SM"></param>
    /// <returns></returns>
    public string AddSceneMonoGetName(string Name, SceneMonoData SM)
    {
        if (!Name.Contains("("))
        {
            Name = Name + " (0)";
        }
        int i = 0;
        string name = Name;
        while (allSceneMono._sceneMonoDatas.ContainsKey(name))
        {
            i++;
            name = Name.Split('(')[0] + "(" + i + ")";
        }
        Name = name;
        allSceneMono._sceneMonoDatas.Add(Name, SM);
        return Name;
    }
    public void AddSceneObj(string Name,out GameObject Clone)
    {
        GameObject clone;
        if (allSceneMono._ObjList.ContainsKey(Name))
        {
            Clone = allSceneMono._ObjList[Name];
            return;
        }
        if (GameObject.Find(Name) != null)
        {
            Clone = GameObject.Find(Name);
            allSceneMono._ObjList.Add(Name, Clone);
            return;
        }
        clone = ResourceManager.Instance.Instantiate("Prefabs/CreatedSceneMono/" + Name.Split('(')[0].Split(' ')[0], GameObject.Find("SceneObjs").transform);
        clone.name = Name;

        if (clone.GetComponent<SceneMonoBase>() != null)
        {
            clone.GetComponent<SceneMonoBase>()._sceneMono = new SceneMonoData();
        }

        Clone = clone;
        allSceneMono._ObjList.Add(Name, clone);
    }
    /// <summary>
    /// 移除一个对象
    /// </summary>
    /// <param name="mono"></param>
    public void RemoveSceneMono(string Name)
    {
        if (!allSceneMono._sceneMonoDatas.ContainsKey(Name))
        {
            //Debug.LogError("MonoList没有" + Name);
            return;
        }
        allSceneMono._sceneMonoDatas.Remove(Name);
    }
    private void RemoveSceneObj(string Name)
    {
        GameObject clone = allSceneMono._ObjList[Name];
        allSceneMono._ObjList.Remove(Name);
        ResourceManager.Instance.Destroy(clone);
    }

    public SceneMonoData GetSceneMono(string Name)
    {
        if (!allSceneMono._sceneMonoDatas.ContainsKey(Name))
        {
            return null;
        }
        return allSceneMono._sceneMonoDatas[Name];
    }

    public void ClearAll()
    {
        allSceneMono._sceneMonoDatas = new Dictionary<string, SceneMonoData>();
    }

    public void SetFlameRecorder(GameObject obj)
    {
        allSceneMono._flameRecorder.Add(obj);
        while (allSceneMono._flameRecorder.Count > Data.Instance.TableAgent.GetInt("Base", "MaxFlame", "Value"))
        {
            allSceneMono._flameRecorder[0].GetComponent<Flame>().EndBurn();
        }
    }
    public void RemoveFromFlameRecorder(GameObject obj)
    {
        for(int i=0;i< allSceneMono._flameRecorder.Count; i++)
        {
            if(allSceneMono._flameRecorder[i].name == obj.name)
            {
                allSceneMono._flameRecorder.RemoveAt(i);
            }
        }
    }
}
public class AllSceneMono : GameDataBase
{
    public Dictionary<string, SceneMonoData> _sceneMonoDatas = new Dictionary<string, SceneMonoData>();
    /// <summary>
    /// 场上的所有SceneMono的Gameobj
    /// </summary>
    public Dictionary<string, GameObject> _ObjList = new Dictionary<string, GameObject>();

    public List<GameObject> _flameRecorder = new List<GameObject>();
}

public enum SceneType
{
    Floor,
    VerWall,
    HorWall,
    Hole,
    Explosion,
    Door,
    Portal,
    Spawner,
    Chest,
    Challenge,
    Obstacle,
}
public enum Shape
{
    Square,
    Circle,
    Rectangle
}
public class SceneMonoData
{
    public Vector3 position;
    public SceneType sceneType;
    public MonoAttribute attr;
    public List<int> Params = new List<int>();
}
